clear
use ElectoralData.dta
log using SelfReportedAndInferenceApproaches.log, replace 


************************************************************************
******              SELF-REPORTED APPROACH          ********************
************************************************************************


***how many individuals vote for PSOE?
sum id if alter==voto & voto==1
****how many voters vote strategically for PSOE?
sum id if alter==voto & voto==1 & votoestrategico==1

***how many individuals vote for PP?
sum id if alter==voto & voto==2
****how many voters vote strategically for PP?
sum id if alter==voto & voto==2 & votoestrategico==1

***How many strategic voters are used in the SELF-REPORTING APPROACH?
sum id if alter==voto & votoestrategico==1


********************************************************************************
***                  INFERENCE APPROACH                       ******************
********************************************************************************

** We use the strategic indicators W1 and W2 defined by Alvarez and Nagler

merge m:1 PROV alter using W1W2.dta

sort id alter


**restrict some coefficients to be zero 

const define 1 [4]siafiliacpartido=0
const define 2 [6]praticareligion1=0
const define 3 [7]siafiliacpartido=0
const define 4 [8]ingresos4=0
const define 5 [13]siafiliacpartido=0

** The random utility model with STRATEGIC INDICATORS

asclogit choice W1ij W2ij W1ijW2ij ParCercano informacion votoanterior, case(id) casevars(ingresos2 ingresos3 ingresos4 estudios praticareligion1 IzqDer evalueconomica evalupolitica siafiliacpartido siafiliacsindicato EDAD sexo) alternatives(alter) const(1,2,3,4,5) basealternative(1) technique(bfgs) vce(robust)
* generates a latex table using outreg2 ado
outreg2 using inferenceapproachModel, tex replace 

***predicted probabilities 
predict p1
**identifying the party with highest probability
by id: egen pro1=max(p1)
** esti1 equals 1 for the alternative with the highest probability and 0 otherwise
gen esti1=0
replace esti1=alter if p1==pro1

*** set W1ij, W2ij and W1ijW2ij equal to 0 to generate predicted sincere vote
replace W1ij=0
replace W2ij=0
replace W1ijW2ij=0 
predict p0
by id: egen pro0=max(p0)
gen esti0=0
replace esti0=alter if p0==pro0

*** identify the individuals who have different predicted sincere and strategic vote
generate votodif=1
replace votodif=0 if esti1==esti0
sum votodif if votodif==1

*** identify the strategic vote
by id: egen strategicvote=max(esti1)

** identify sincere vote 
by id: egen sincvote=max(esti0)
 
*** where does the strategic vote to PSOE come from? (what would PSOE's strategic voters have voted, had their vote been sincere?)
*from PP
sum id if esti1==1 & votodif==1 & sincvote==2
*from IU
sum id if esti1==1 & votodif==1 & sincvote==3 
*from UpyD
sum id if esti1==1 & votodif==1 & sincvote==4
*from Ciu
sum id if esti1==1 & votodif==1 & sincvote==5
*from Erc
sum id if esti1==1 & votodif==1 & sincvote==6
*from Pnv
sum id if esti1==1 & votodif==1 & sincvote==7
*from Bng
sum id if esti1==1 & votodif==1 & sincvote==8
*from Cc
sum id if esti1==1 & votodif==1 & sincvote==9
*from Abstein
sum id if esti1==1 & votodif==1 & sincvote==13 

*** where does the strategic vote of PP come from? (what would PP's strategic voters have voted, had their vote been sincere?)
*from PSOE
sum id if esti1==2 & votodif==1 & sincvote==1
*from IU
sum id if esti1==2 & votodif==1 & sincvote==3 
*from UpyD
sum id if esti1==2 & votodif==1 & sincvote==4
*from Ciu
sum id if esti1==2 & votodif==1 & sincvote==5
*from Erc
sum id if esti1==2 & votodif==1 & sincvote==6
*from Pnv
sum id if esti1==2 & votodif==1 & sincvote==7
*from Bng
sum id if esti1==2 & votodif==1 & sincvote==8
*frome Cc
sum id if esti1==2 & votodif==1 & sincvote==9
*frome Abstein
sum id if esti1==2 & votodif==1 & sincvote==13 

** So the STRATEGIC VOTE using the INFERENCER APPROACH IS...

sum id if esti1==1 & votodif==1
sum id if esti1==2 & votodif==1
 
log close
